package com.example.seckill.json;

import cn.hutool.core.util.ClassUtil;
import com.example.seckill.error.CommonErrorEnum;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lombok.SneakyThrows;

import java.io.IOException;
import java.lang.reflect.Field;

public class CommonErrorEnumSerializer extends JsonSerializer<CommonErrorEnum> {

    @SneakyThrows
    @Override
    public void serialize(CommonErrorEnum commonErrorEnum, JsonGenerator generator, SerializerProvider provider) throws IOException {
        Field[] declaredFields = ClassUtil.getDeclaredFields(CommonErrorEnum.class);
        for (Field declaredField : declaredFields) {
            Class<?> type = declaredField.getType();
            System.out.println(type);
            System.out.println(declaredField.getName());
        }
        generator.writeStartObject();
        generator.writeFieldName("errCode");
        generator.writeNumber(commonErrorEnum.getErrCode());
        generator.writeFieldName("errMsg");
        generator.writeString(commonErrorEnum.getErrMsg());
        generator.writeEndObject();
    }
}